﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UltimatePlugin.Technicals
{
    internal class PerformanceLogger : IDisposable
    {
        private Stopwatch _Stopwatch;
        private string _BlockName;
        private LogSeverity _Severity;

        public PerformanceLogger(LogSeverity severity, string blockName, params object[] args)
        {
            _BlockName = blockName;
            _Severity = severity;
            _Stopwatch = new Stopwatch();
            Logger.Write(_Severity, LogCategory.Performance, "Starting Block {0}", _BlockName);
            Logger.Indent(LogCategory.Performance);
            _Stopwatch.Start();
            
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
            Dispose(true);
        }

        private void Dispose(bool disposing)
        {
            _Stopwatch.Stop();
            Logger.Unindent(LogCategory.Performance);
            Logger.Write(_Severity, LogCategory.Performance, "Ending Block {0} in {1} ms", _BlockName, _Stopwatch.ElapsedMilliseconds);
        }

        ~PerformanceLogger()
        {
            Dispose(false);
        }
    }
}
